/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import sun.misc.BASE64Encoder;

/**
 *
 * @author Jeremy
 */
@Entity
public class Account implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
  private String name;
  private boolean administrator;
  private boolean damaged;
  private String hashedPassword;
  private String emailAddress;
  private String salt;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (null != id ? id.hashCode() : 0) +
            (null != name ? name.hashCode() : 0) +
            ((new Boolean(administrator)).hashCode()) +
            ((new Boolean(damaged)).hashCode()) +
            (null != hashedPassword ? hashedPassword.hashCode() : 0) +
            (null != emailAddress ? emailAddress.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    if (!(object instanceof Account)) {
      return false;
    }
    Account other = (Account) object;
    if ((null == this.id && null != other.id) ||
        (null != this.id && !this.id.equals(other.id))) {
      return false;
    }
    if ((null == this.name && null != other.name) ||
        (null != this.name && !this.name.equals(other.name))) {
      return false;
    }
    if (this.administrator != other.administrator) {
      return false;
    }
    if (this.damaged != other.damaged) {
      return false;
    }
    if ((null == this.hashedPassword && null != other.hashedPassword) ||
        (null != this.hashedPassword &&
         !this.hashedPassword.equals(other.hashedPassword))) {
      return false;
    }
    if ((null == this.emailAddress && null != other.emailAddress) ||
        (null != this.emailAddress &&
         !this.emailAddress.equals(other.emailAddress))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "jhu.profiteer.data.Account[id=" + id +
           ", name=" + name +
           ", administrator=" + administrator +
           ", damaged=" + damaged +
           ", emailAddress=" + emailAddress + "]";
  }

  /**
   * @return the name
   */
  public String getName() {
    return name;
  }

  /**
   * @param name the name to set
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * @return the administrator
   */
  public boolean isAdministrator() {
    return administrator;
  }

  /**
   * @param administrator the administrator to set
   */
  public void setAdministrator(boolean administrator) {
    this.administrator = administrator;
  }

  /**
   * @return the damaged
   */
  public boolean isDamaged() {
    return damaged;
  }

  /**
   * @param damaged the damaged to set
   */
  public void setDamaged(boolean damaged) {
    this.damaged = damaged;
  }

  /**
   * @return the emailAddress
   */
  public String getEmailAddress() {
    return emailAddress;
  }

  /**
   * @param emailAddress the emailAddress to set
   */
  public void setEmailAddress(String emailAddress) {
    this.emailAddress = emailAddress;
  }

  public void setPassword(String password) {
    this.salt = UUID.randomUUID().toString().substring(0, 5);
    String temp = password + this.salt;

    try {
      MessageDigest md = MessageDigest.getInstance("SHA");
      md.update(temp.getBytes("UTF-8"));
      byte[] raw = md.digest();
      this.hashedPassword = (new BASE64Encoder()).encode(raw);
    } catch (UnsupportedEncodingException ex) {
      System.out.println(ex);
    } catch (NoSuchAlgorithmException ex) {
      System.out.println(ex);
    }
  }

  public boolean verifyPassword(String password) {
    String temp = password + this.salt;
    byte[] raw = null;

    try {
      MessageDigest md = MessageDigest.getInstance("SHA");
      md.update(temp.getBytes("UTF-8"));
      raw = md.digest();
    } catch (UnsupportedEncodingException ex) {
      System.out.println(ex);
    } catch (NoSuchAlgorithmException ex) {
      System.out.println(ex);
    }

    return this.hashedPassword.equalsIgnoreCase((new BASE64Encoder()).encode(raw));
  }
}
